package org.lds.message.center.common.model;

import lombok.AllArgsConstructor;
import lombok.Data;
import org.lds.message.center.common.enums.ErrCodeEnum;

import java.io.Serializable;

/**
 * 登陆统一返回实体类
 *
 * @author lidongsheng 2021/3/10 16:52
 * @version V1.0
 */
@Data
@AllArgsConstructor
public class ResultDTO<T> implements Serializable {
    private static final long serialVersionUID = 1L;

    /**
     * 错误码
     */
    private String code;

    /**
     * 错误信息
     */
    private String message;

    /**
     * 是否成功
     */
    private boolean success;

    /**
     * 响应结果
     */
    private T data;

    private ResultDTO() {
    }

    private ResultDTO(String code, String message, T obj) {
        this.code = code;
        this.message = message;
        this.success = false;
        this.data = obj;
    }

    private ResultDTO(T obj) {
        this.success = true;
        this.data = obj;
    }

    public static <T> ResultDTO<T> result(String errorCode, String errorMsg, boolean success, T obj) {
        return new ResultDTO(errorCode, errorMsg, success, obj);
    }

    public static <T> ResultDTO<T> success() {
        return new ResultDTO(null);
    }

    public static <T> ResultDTO<T> success(T obj) {
        return new ResultDTO(obj);
    }

    public static <T> ResultDTO<T> fail(String errCode, String errMsg) {
        return new ResultDTO(errCode, errMsg, null);
    }

    public static <T> ResultDTO<T> fail(ErrCodeEnum errCodeEnum) {
        return new ResultDTO(errCodeEnum.getErrCode(), errCodeEnum.getErrMsg(), null);
    }

}
